package com.jerry.huihome.module.find.fragment;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.huihome.R;
import com.jerry.huihome.base.BaseFragment;
import com.jerry.huihome.constant.Urls;
import com.jerry.huihome.module.find.adapter.ForemanDynamicAdapter;
import com.jerry.huihome.module.find.entity.ForemanDynamic;
import com.jerry.huihome.utils.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2016/5/13.
 */
public class ForemanDynamicFragment extends BaseFragment {
    @BindView(R.id.ptf_foreman_dynamic)
    PullToRefreshListView ptfForemanDynamic;
    private String url;
    private String pmid;
    private int pageIndex = 1;
    private List<ForemanDynamic.BodyBean.DynaItemsBean> dynaItemsBeen = new ArrayList<>();
    private ForemanDynamicAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_froeman_dynamic;
    }

    @Override
    protected void initViews() {
        ptfForemanDynamic.getRefreshableView().setDivider(null);
        ptfForemanDynamic.getRefreshableView().setSelector(android.R.color.transparent);
        ptfForemanDynamic.setMode(PullToRefreshBase.Mode.BOTH);
        ptfForemanDynamic.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex = 1;
                loadData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                pageIndex++;
                loadData();
            }
        });
    }

    @Override
    protected void getReqParams(Bundle bundle) {
        pmid = bundle.getString("pmid");
    }

    @Override
    protected void loadData() {
        url = Urls.FOREMAN_DYNAMIC_URL + pmid +"&pageIndex="+pageIndex;
        OkHttpUtils.get(url, new OkHttpUtils.OnGetDataListener() {
            @Override
            public void onResponse(String url, String data) {
                if (data != null){
                    if (pageIndex == 1){
                        if (adapter != null){
                            adapter.clear();
                        }
                    }
                    Gson gson = new Gson();
                    ForemanDynamic foremanDynamic = gson.fromJson(data, ForemanDynamic.class);
                    List<ForemanDynamic.BodyBean.DynaItemsBean> dynaItems = foremanDynamic.getBody().getDynaItems();
                    if (dynaItems != null){
                        dynaItemsBeen.addAll(dynaItems);
                        showAdapter();
                    }else {
                        Toast.makeText(getContext(),"没有更多内容哟",Toast.LENGTH_LONG).show();
                    }
                }
            }
            @Override
            public void onFailure(String url, String error) {

            }
        });
    }

    private void showAdapter() {
        adapter = new ForemanDynamicAdapter(getContext(),dynaItemsBeen);
        ptfForemanDynamic.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        ptfForemanDynamic.onRefreshComplete();
    }
}
